package com.bree.erp.jsf;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;

import com.bree.erp.entity.PurchaseRequisition;
import com.bree.erp.jsf.httpsess.SessionDataKey;
import com.bree.erp.jsf.httpsess.UserSessionData;
import com.bree.erp.jsf.vo.ItemRequestDT;
import com.bree.erp.service.PurchaseRequisitionService;

@ManagedBean(name = "purchaseOrderBean")
@RequestScoped
public class PurchaseOrderBean implements Serializable {
	
	private static final long serialVersionUID = 1L;

	private static Logger LOG = Logger.getLogger(PurchaseOrderBean.class);

	@ManagedProperty(value = "#{userSession}")
	private UserSessionData userSession;
	
	@EJB
	private PurchaseRequisitionService prService;

	private PurchaseRequisition purchaseRequisition;
	
	private List<ItemRequestDT> requests;

	@PostConstruct
	public void initPurchaseRequisition() {
		
		Long id = userSession.get(
				SessionDataKey.SELECTED_PURCHASE_REQUISITION_ID, Long.class);
		
		LOG.debug("Initializing with PR#: " + id);

		if (id != null) {
			purchaseRequisition = prService.read(id, true);
		}
		
		
	}
	
	/**
	 * @return the purchaseRequisition
	 */
	public PurchaseRequisition getPurchaseRequisition() {
		return purchaseRequisition;
	}

	/**
	 * @param purchaseRequisition the purchaseRequisition to set
	 */
	public void setPurchaseRequisition(PurchaseRequisition purchaseRequisition) {
		this.purchaseRequisition = purchaseRequisition;
	}

	/**
	 * @return the userSession
	 */
	public UserSessionData getUserSession() {
		return userSession;
	}

	/**
	 * @param userSession the userSession to set
	 */
	public void setUserSession(UserSessionData userSession) {
		this.userSession = userSession;
	}

	/**
	 * @return the requests
	 */
	public List<ItemRequestDT> getRequests() {
		return requests;
	}
}
